<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="<%=request.getContextPath()%>/css/base.css" rel="stylesheet" type="text/css" />
<link href="<%=request.getContextPath()%>/css/admin.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery.tools.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery.validate.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery.validate.methods.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/base.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/admin.js"></script>
<script charset="utf-8" src="<%=request.getContextPath()%>/common/kindeditor/kindeditor.js"></script>
<script src="<%=request.getContextPath()%>/js/jquery.uploadify.v2.1.4.min.js" type="text/javascript"></script>
<script src="<%=request.getContextPath()%>/js/swfobject.js" type="text/javascript"></script>
<link href="<%=request.getContextPath()%>/js/uploadify.css" rel="stylesheet" type="text/css" />

</head>


<BODY class="input goods" sizcache="0" sizset="0" ><DIV class=bar>修改商品:${goods.goodsName} </DIV>
<div id="validateErrorContainer" class="validateErrorContainer">
		<div class="validateErrorTitle">以下信息填写有误,请重新填写</div>
		<ul></ul>
	</div>

<DIV class=body sizcache="0" sizset="0">

<UL id=tab class=tab>
<LI><INPUT hideFocus class=current value=基本信息 type=button> </LI>
<LI><INPUT hideFocus value=商品描述 type=button> </LI>
<LI><INPUT hideFocus value=商品图片 type=button> </LI>

</UL>
<FORM id=goodsForm class="tabContent" encType=multipart/form-data method=post action=goodssave.do sizcache="0" sizset="0" >
<TABLE  id=infoTable class="inputTable" sizcache="0" sizset="0" >
<TBODY sizcache="0" sizset="0">
<TR>
<TH>商品分类: </TH>
<input type="hidden" name=id value=${goods.id}>
<input type="hidden" name=uuid value=${goods.UUID}>
<TD><SELECT name=typeid onchange="changebrand(this)"> 
<OPTION value="">请选择...</OPTION> 
<c:forEach items="${toptypes}" var="goodstype">
       <OPTION value=${goodstype.id} ${goods.goodType.id==goodstype.id?'selected':''}>${goodstype.typeName}</OPTION> 
          <c:forEach items="${goodstype.underGoodTypes}" var="subgoodstype">
            <OPTION value=${subgoodstype.id} ${goods.goodType.id==subgoodstype.id?'selected':''}>&nbsp;&nbsp;&nbsp;&nbsp;${subgoodstype.typeName}</OPTION> 
            <c:forEach items="${subgoodstype.underGoodTypes}" var="tgoodstype">
              <OPTION value=${tgoodstype.id} ${goods.goodType.id==tgoodstype.id?'selected':''}>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;${tgoodstype.typeName}</OPTION> 
            </c:forEach>
          </c:forEach>
      </c:forEach>
</SELECT> <LABEL class=requireField>*</LABEL> </TD></TR>
<TR>
<TH>商品名称: </TH>
<TD><INPUT class=formText type=text name="goodsName" id=goodsName value=${goods.goodsName}> <LABEL class=requireField>*</LABEL> </TD></TR>
<TR>
<TH>商品编号: </TH>
<TD><INPUT class=formText title=商品条码 type=text name=goodsNO value=${goods.goodsNO}> <LABEL class=requireField>*</LABEL> </TD></TR>
<TR>
<TH>品牌: </TH>
<TD><SELECT name=brandid id=brandid> 
     <OPTION selected value="">请选择...</OPTION> 
     <c:forEach items="${brands}" var="brand">
       <OPTION value=${brand.id} ${goods.brand.id==brand.id?'selected':''}>${brand.brandName}</OPTION> 
     </c:forEach>
</SELECT> </TD></TR>
<TR class=specificationDisabledInfo>
<TH>销售价: </TH>
<TD><INPUT class="formText valid"  type=text name=price value=${goods.price}> <LABEL class=requireField>*</LABEL> </TD></TR>
<TR class=specificationDisabledInfo>
<TH>成本价: </TH>
<TD><INPUT class=formText  type=text name=costPrice value=${goods.costPrice}> <LABEL class=requireField>*</LABEL> </TD></TR>
<TR class=specificationDisabledInfo>
<TH>市场价: </TH>
<TD><INPUT class=formText  type=text name=historyPrice value=${goods.historyPrice}> <LABEL class=requireField>*</LABEL> </TD></TR>
<TR class=specificationDisabledInfo>
<TH>库存: </TH>
<TD><INPUT class=formText title=只允许输入零或正整数,为空表示不计库存 type=text name=goodsNum value=${goods.goodsNum}> <LABEL class=requireField>*</LABEL> </TD></TR>
<TR class=specificationDisabledInfo>
<TH>排序: </TH>
<TD><INPUT class=formText title=只允许输入零或正整数 type=text name=sort value=${goods.sort}> <LABEL class=requireField>*</LABEL> </TD></TR>
<TR sizcache="0" sizset="0">
<TH>设置: </TH>
<TD sizcache="0" sizset="0">
<LABEL sizcache="0" sizset="0"><INPUT id=goodsIsMarketable value=1 ${goods.isMarketable==1?'CHECKED':''}  type=checkbox name=isMarketable>上架 </LABEL>
<LABEL sizcache="0" sizset="1"><INPUT id=goods_isBest value=1 ${goods.isBest==1?'CHECKED':''} type=checkbox name=isBest>精品 </LABEL>
<LABEL sizcache="0" sizset="2"><INPUT id=goods_isNew value=1 ${goods.isNew==1?'CHECKED':''} type=checkbox name=isNew>新品 </LABEL>
<LABEL sizcache="0" sizset="3"><INPUT id=goods_isHot value=1 ${goods.isHot==1?'CHECKED':''} type=checkbox name=isHot>热销 </LABEL>
</TD>
</TR>
<TR>
<TH>页面关键词: </TH>
<TD><INPUT class=formText type=text name=keyWorld value=${goods.keyWorld}> </TD></TR>
<TR>
<TH>页面描述: </TH>
<TD><TEXTAREA class=formTextarea name=description>${goods.description}</TEXTAREA> </TD></TR></TBODY></TABLE>
<DIV class=buttonArea><INPUT hideFocus class=formButton value="确  定" type=submit>&nbsp;&nbsp; <INPUT hideFocus class=formButton onclick="back()" value="返  回" type=button> </DIV>
<INPUT id=searchKey type=hidden name=searchKey value=${searchKey} > 
<INPUT id=type type=hidden name=type value=${type} > 
<INPUT id=pageSize type=hidden name=page.pageSize value=${page.pageSize} > 
<INPUT id=pageNumber type=hidden name=page.pageNumber value=${page.currentPage} > 
<INPUT id=totalSize type=hidden name=page.totalSize value=${page.totalRowsAmount} > 
<INPUT id=orderBy type=hidden name=pager.orderBy value=${page.orderBy} > 
<INPUT id=order  type=hidden name=pager.order value=${page.order}> 
</FORM>
<TABLE  class="inputTable tabContent"  >
<TBODY>
<TR>
<TD>
<textarea class="editor" style="width:100%;height:1000px" id="editor" name="editor" >${editor}</textarea>
</TD></TR>
<TR class=buttonArea><TD>
<INPUT hideFocus class=formButton value="确  定" type=button onclick="savedesc()">&nbsp;&nbsp; 
<INPUT hideFocus class=formButton onclick="back()" value="返  回" type=button> 
</TD></TR>
</TBODY>
</TABLE>


<TABLE  id=goodsImageTable class="inputTable tabContent" >
<TBODY>
<TR class=noneHover>
<TD colSpan=5><INPUT type="file" name="uploadify" id="uploadify"  /></TD></TR>
<tr class="goodsImageTr">
<c:forEach items="${fileNames}" var="fileName">
<td> <img src="${fileName}"  width="120px"> <span class="deleteIcon deleteGoodsImage" title="删 除">&nbsp;</span> </td> 
</c:forEach>
</tr>
<TR class=buttonArea><TD>
<INPUT hideFocus class=formButton value="确  定" type=button onclick="saveImages()">&nbsp;&nbsp; 
<INPUT hideFocus class=formButton onclick="back()" value="返  回" type=button> 
</TD></TR></TBODY></TABLE>

</DIV>


</BODY>

<script type="text/javascript">
var hasEdit=false;
KindEditor.ready(function(K) {
	K.create('textarea[name="editor"]', {
	    uploadJson : '<%=request.getContextPath()%>/common/kindeditor/jsp/upload_json.jsp?id=${goods.UUID}&type=goods',
		fileManagerJson : '<%=request.getContextPath()%>/common/kindeditor/jsp/file_manager_json.jsp?id=${goods.UUID}&type=goods',
		allowFileManager : true,
		afterBlur:function(){ 
            this.sync(); }    
		
	});
});


$().ready(function() {
    var $validateErrorContainer = $("#validateErrorContainer");
	var $validateErrorLabelContainer = $("#validateErrorContainer ul");
	var $tab = $("#tab");
	var $goodsForm = $("#goodsForm");
	var $goodsImageTr = $("#goodsImageTable .goodsImageTr");
     // Tab效果
	$tab.tabs(".tabContent", {
		tabs: "input", onBeforeClick:function(index){  
            <c:if test="${empty goods}">
            if(index.srcElement)
            {
		     $.dialog({type: "warn", content: "请先保存商品基本信息!", modal: true, autoCloseTime: 3000});
		     return false;
		    }
		    </c:if>			
           return true;  
    }
	});
   
    
	
	// 删除商品图片
	$("#goodsImageTable .deleteGoodsImage").live("click", function(){
		var $this = $(this);
		$.dialog({type: "warn", content: "您确定要删除吗?", ok: "确 定", cancel: "取 消", modal: true, okCallback: deleteGoodsImage});
		function deleteGoodsImage() {
			$this.parent().remove();
			var imgsrc=$this.prev().attr("src");
			var filename=imgsrc.substring(imgsrc.lastIndexOf("/")+1,imgsrc.length);
			var url='<%=request.getContextPath()%>/admin/imgDel.do?id=${goods.id}&imgname='+filename;
	    	$.get(url);
		}
	});
    
	// 表单验证
	$goodsForm.validate({	   
		errorClass: "validateError",   
		rules: {
			typeid: "required",
			goodsName: "required",	
			goodsNO: {required: true,digits: true},	
			price: {required: true,number: true},	
			costPrice: {required: true,number: true},	
			historyPrice: {required: true,number: true},		
			goodsNum: {required: true,digits: true},	
			sort: {required: true,digits: true}		
		},
		messages: {
			typeid: "请选择商品类别!",
			goodsName: "请填写商品名称!",	
			goodsNO:{required:"请填写商品编号!",digits:"商品编号必需是正整数!"},	
			price:{required:"请填写商品售价!",number:"请填写合法的数字!"},				
			costPrice:{required:"请填写商品成本价!",number:"请填写合法的数字!"},		
			historyPrice:{required:"请填写商品市场价!",number:"请填写合法的数字!"},		
			goodsNum:{required:"请填写商品库存!",digits:"请填写合法的数字!"},
			sort:{required:"请填写商品排序!",digits:"请填写合法的数字!"}
		},
		submitHandler: function(form) {			   
			$(form).find(":submit").attr("disabled", true);
			form.submit();
		},
        errorPlacement: function(error, element) { 
            error.appendTo(element.parent()); 
        }
	});
	
	//初始化上传按钮
	$("#uploadify").uploadify( {//初始化函数

    'uploader' :'../js/uploadify.swf',//flash文件位置，注意路径
    'script' :'<%=request.getContextPath()%>/admin/imgUpload.do',//后台处理的请求
    'cancelImg' :'../js/cancel.png',//取消按钮图片
    'folder' :'uploads',//您想将文件保存到的路径
    'queueID' :'fileQueue',//与下面的上传文件列表id对应
    'queueSizeLimit' :10,//上传文件的数量
    'scriptData':{'goodsid':'${goods.id}'},//向后台传的数据
    'fileDesc' :'jpg、png和gif文件',//上传文件类型说明
    'fileExt' :'*.jpg;*.jpeg;*.png;*.gif', //控制可上传文件的扩展名，启用本项时需同时声明fileDesc
    'method':'post',//如果向后台传输数据，必须是get
    'sizeLimit':10000000,//文件上传的大小限制，单位是字节
    'auto' :true,//是否自动上传
    'multi' :true,
    'simUploadLimit' :10,//同时上传文件的数量
    //'buttonText' :'上传',//浏览按钮图片
    'onComplete': function(event, queueID, fileObj,response,data) {//当上传完成后的回调函数，ajax方式哦~~ 
       var goodsImageHtml = '<td> <img src="'+response+'" width="120px"><span class="deleteIcon deleteGoodsImage" title="删 除">&nbsp;</span> </td>';		
	   $goodsImageTr.append(goodsImageHtml);
    }
   });		
})

  function savedesc()
  {
  var s = $("#editor").val();
  var url='<%=request.getContextPath()%>/admin/goodsdescsave.do?id=${goods.id}';
  $.post(url,{editor:s},function(result){
        hasEdit=true;
        $.dialog({type: "success", content: "成功保存商品描述信息!", modal: true, autoCloseTime: 3000});
  });  
  }
  function saveImages()
  {
    var url='<%=request.getContextPath()%>/admin/goodsimgsave.do?id=${goods.id}';
    $.get(url,function(result){
         hasEdit=true;
         $.dialog({type: "success", content: "成功保存商品图片信息!", modal: true, autoCloseTime: 3000});
    });
  }
  
  function changebrand(type)
 {
   $("#brandid").empty();
   $("#brandid").prepend("<option value=''>请选择...</option>");  
   var typeid=type.value;
   if(typeid!='')
   { 
    var url='<%=request.getContextPath()%>/admin/querybrand.do?typeId='+typeid;
     $.get(url, function(result){	
        var brands=eval('('+result+')');	 
         for(i=0;i<brands.length;i++)
        {
          $("#brandid").append("<option value='"+brands[i].id+"'>"+brands[i].name+"</option>");   
        }
	   });   
   }
 }
 
  function back()
  {
    if(hasEdit)
    {
    $.dialog({type: "warn", content: "商品做过修改，需要更新缓存吗?", ok: "确 定", cancel: "取 消", modal: true, okCallback: updteHtml,cancelCallback:cancel});
		function updteHtml() {		
		  var url='<%=request.getContextPath()%>/admin/saveGoodsHtml.do?id=${goods.id}';
		  $.get(url,function(result){
		     $.dialog({type: "success", content: "更新缓存成功！", ok: "确 定", modal: true, okCallback: cancel});
		  });		 
		}
		function cancel()
		{
		  window.history.back(); return false;
		}
    }
    else
    {
    window.history.back(); return false;
    }
  }
</script>
</html>